/*
 * TelaAluguel.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.aluguel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import business.aluguel.Aluguel;
import business.aluguel.Cliente;
import business.aluguel.PosicaoAluguel;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import relatorios.GeraRelatorio;
import telas.comum.TelaMDI;

/**
 *
 * @author  __USER__
 */
public class TelaAluguel extends TelaMDI
{
	private static final long serialVersionUID = 1844736229788851470L;

	/** Creates new form TelaAluguel */
	public TelaAluguel() throws Exception
	{
		initComponents();

		eventoBuscar();
		eventoNovo();
		eventoEditar();
		eventoExcluir();
	}

	private void eventoExcluir()
	{
		barraFerramenta1.getBtExcluir().addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					exluirAluguel();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void exluirAluguel() throws Exception
	{
		Aluguel aluguel = (Aluguel) tblContrato.getObjetoCorrente();

		aluguel.excluir();

		preencheTabelaContrato();
	}

	private void eventoEditar()
	{
		barraFerramenta1.getBtEditar().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(false);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void eventoNovo()
	{
		barraFerramenta1.getBtNovo().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(true);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void abrirTela(boolean novo) throws Exception
	{
		Aluguel aluguel;

		if (novo)
		{
			aluguel = new Aluguel();
		} else
		{
			aluguel = (Aluguel) tblContrato.getObjetoCorrente();
		}

		new TelaAluguelCadastro(aluguel, novo);

		preencheTabelaContrato();
	}

	private void eventoBuscar()
	{
		barraFerramenta1.getBtBusca().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					preencheTabelaContrato();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void preencheTabelaContrato() throws Exception
	{
		List<IObjetoSistema> aluguel = new Aluguel()
				.obterTodosPorFiltro(filtro());

		tblContrato.setDados(aluguel);
		tblContrato.setColunas(colunasTabelaAlugueis());
		tblContrato.bind();
	}

	private String filtro() throws Exception
	{
		String criterio = "1 = 1";

		if (!ctCodigoContrato.getText().equals(""))
		{
			criterio += " AND this_.codigo = " + ctCodigoContrato.getText();
		}

		PosicaoAluguel posicaoAluguel = (PosicaoAluguel) cgPosicaoContrato
				.getObjetoCorrente();
		if (posicaoAluguel != null)
		{
			criterio += " AND this_.codigo_posicao_aluguel = "
					+ posicaoAluguel.getCodigo();
		}

		Cliente cliente = (Cliente) cplCliente.getObjeto();
		if (cliente.getCodigo() != 0)
		{
			criterio += " AND this_.codigo_cliente = " + cliente.getCodigo();
		}

		if (!ctPlacaVeículo.getText().equals("   -    "))
		{
			criterio += " AND placa = " + ctPlacaVeículo.getText();
		}

		return criterio;
	}

	private ArrayList<String[]> colunasTabelaAlugueis()
	{
		ArrayList<String[]> dscColunas = new ArrayList<String[]>();
		dscColunas.add(new String[]
		{ "Cliente", "getNomeCliente", "300" });
		dscColunas.add(new String[]
		{ "Data Saida", "getDataSaida" });
		dscColunas.add(new String[]
		{ "Data Devolução", "getDataDevolucaoPrevista" });
		dscColunas.add(new String[]
		{ "Situação", "getNomePosicaoAluguel" });
		dscColunas.add(new String[]
		{ "Valor", "calcular" });

		return dscColunas;
	}

	private ArrayList<String[]> formatarGridPesquisaCliente()
	{
		ArrayList<String[]> descricaoColunas = new ArrayList<String[]>();
		descricaoColunas.add(new String[]
		{ "Cliente", "getNome", "300" });
		descricaoColunas.add(new String[]
		{ "CPF", "getCpf" });

		return descricaoColunas;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		pnlPrincipal = new telas.componentes.Painel();
		barraFerramenta1 = new telas.componentes.BarraFerramenta();
		pnlCadastro = new telas.componentes.Painel();
		telas.componentes.Painel pnlFiltro = new telas.componentes.Painel();
		rotulo1 = new telas.componentes.Rotulo();
		rotulo2 = new telas.componentes.Rotulo();
		rotulo3 = new telas.componentes.Rotulo();
		ctCodigoContrato = new telas.componentes.CampoNumeroInteiro();
		cplCliente = new telas.componentes.CampoPopupLocalizador();
		ctPlacaVeículo = new telas.componentes.CampoTexto();
		rotulo4 = new telas.componentes.Rotulo();
		cgPosicaoContrato = new telas.componentes.ComboGeral();
		btImprimri = new telas.componentes.Botao();
		jspTabelaContratos = new javax.swing.JScrollPane();
		tblContrato = new telas.componentes.Tabela();

		setTitle("Aluguel de Ve\u00edculos");

		pnlPrincipal.setLayout(new java.awt.BorderLayout());

		barraFerramenta1.setRollover(true);
		pnlPrincipal.add(barraFerramenta1, java.awt.BorderLayout.PAGE_START);

		pnlCadastro.setLayout(new java.awt.BorderLayout());

		pnlFiltro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		rotulo1.setText("Contrato:");

		rotulo2.setText("Cliente:");

		rotulo3.setText("Ve\u00edculo:");

		cplCliente.setAtributoVisualizacao("Nome");
		cplCliente.setCampoPesquisa("nome");
		cplCliente.setClassePesquisa("business.aluguel.Cliente");
		cplCliente.setDescricaoPesquisa("Nome:");
		cplCliente.setFormatoGridPesquisa(formatarGridPesquisaCliente());

		try
		{
			ctPlacaVeículo
					.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
							new javax.swing.text.MaskFormatter("AAA-####")));
		} catch (java.text.ParseException ex)
		{
			ex.printStackTrace();
		}

		rotulo4.setText("Posi\u00e7\u00e3o:");

		cgPosicaoContrato.setModel(new javax.swing.DefaultComboBoxModel(
				new String[]
				{ "Selecione" }));
		cgPosicaoContrato.setMetodoDescricao("getNome");
		cgPosicaoContrato.setMetodoIdentificador("getCodigo");
		cgPosicaoContrato.setDataSource(posicaoAluguel.obterTodos());

		btImprimri.setText("Imprimir");
		btImprimri.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				btImprimriActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout pnlFiltroLayout = new javax.swing.GroupLayout(
				pnlFiltro);
		pnlFiltro.setLayout(pnlFiltroLayout);
		pnlFiltroLayout
				.setHorizontalGroup(pnlFiltroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								pnlFiltroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																btImprimri,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																96,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																pnlFiltroLayout
																		.createSequentialGroup()
																		.addGroup(
																				pnlFiltroLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								rotulo1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								rotulo2,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								rotulo3,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				pnlFiltroLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								pnlFiltroLayout
																										.createSequentialGroup()
																										.addComponent(
																												ctCodigoContrato,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												137,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												rotulo4,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												cgPosicaoContrato,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												153,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addComponent(
																								cplCliente,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								724,
																								Short.MAX_VALUE)
																						.addComponent(
																								ctPlacaVeículo,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								135,
																								javax.swing.GroupLayout.PREFERRED_SIZE))))
										.addContainerGap()));
		pnlFiltroLayout
				.setVerticalGroup(pnlFiltroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlFiltroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctCodigoContrato,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cgPosicaoContrato,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																pnlFiltroLayout
																		.createSequentialGroup()
																		.addGap(
																				6,
																				6,
																				6)
																		.addComponent(
																				rotulo2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																cplCliente,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctPlacaVeículo,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												btImprimri,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap()));

		pnlCadastro.add(pnlFiltro, java.awt.BorderLayout.PAGE_START);

		jspTabelaContratos.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		tblContrato.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][]
				{

				}, new String[]
				{

				}));
		jspTabelaContratos.setViewportView(tblContrato);

		pnlCadastro.add(jspTabelaContratos, java.awt.BorderLayout.CENTER);

		pnlPrincipal.add(pnlCadastro, java.awt.BorderLayout.CENTER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 815,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 480,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void btImprimriActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			imprimir();
		} catch (Exception ex)
		{
			new ExceptionSistema(ex);
		}
	}

	private void imprimir() throws Exception
	{
		Aluguel aluguel = (Aluguel) tblContrato.getObjetoCorrente();

		List<IObjetoSistema> lista = new ArrayList<IObjetoSistema>();

		lista.add(aluguel);

		Map<Object, Object> parametros = new HashMap<Object, Object>();
		parametros.put("SUBREPORT_DIR", "Relatorios/");

		GeraRelatorio.gerarRelatorioBeans("relatorioContrato", lista, parametros);
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.BarraFerramenta barraFerramenta1;
	private telas.componentes.Botao btImprimri;
	private telas.componentes.ComboGeral cgPosicaoContrato;
	private telas.componentes.CampoPopupLocalizador cplCliente;
	private telas.componentes.CampoNumeroInteiro ctCodigoContrato;
	private telas.componentes.CampoTexto ctPlacaVeículo;
	private javax.swing.JScrollPane jspTabelaContratos;
	private telas.componentes.Painel pnlCadastro;
	private telas.componentes.Painel pnlPrincipal;
	private telas.componentes.Rotulo rotulo1;
	private telas.componentes.Rotulo rotulo2;
	private telas.componentes.Rotulo rotulo3;
	private telas.componentes.Rotulo rotulo4;
	private telas.componentes.Tabela tblContrato;
	// End of variables declaration//GEN-END:variables

	private PosicaoAluguel posicaoAluguel = new PosicaoAluguel();
}